﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp1.Utils
{
    internal class StringUtils
    {
        public static byte[] strToHexByte(string hexString)
        {
            if (hexString == null) return null;
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }

        public static byte[] hexStringToBytes(String hexString)
        {
            if (hexString != null && !hexString.Equals(""))
            {
                hexString = hexString.ToUpper();
                int length = hexString.Length / 2;
                char[] hexChars = hexString.ToCharArray();
                byte[] d = new byte[length];

                for (int i = 0; i < length; ++i)
                {
                    int pos = i * 2;
                    d[i] = (byte)(charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
                }

                return d;
            }
            else
            {
                return null;
            }
        }
        public static byte charToByte(char c)
        {
            if ("0123456789ABCDEFabcdef".IndexOf(c)>=0)
            {
                return (byte)"0123456789ABCDEFabcdef".IndexOf(c);
            }
            return 0;
        }

        public static String asciiToString(String value)
        {

            StringBuilder sbu = new StringBuilder();

            for (int i = 0; i < value.Length; i++)
            {
                //System.out.println("chars[i]="+chars[i]);
                String s = String.Format("{0:x2}", (byte)value[i]);
                sbu.Append(s);
            }
            return sbu.ToString();
        }
        public static string StringToASCII(string value)
        {
            StringBuilder s = new StringBuilder();
            var bytes = stringToAsciiArr(value);
            foreach (var item in bytes)
            {
                s.Append(item.ToString("X2"));
            }
            return s.ToString();
        }
        public static byte[] stringToAsciiArr(String value)
        {
            return System.Text.Encoding.Default.GetBytes(value);
        }

        /// <summary>
        /// 字符串转16进制字符
        /// </summary>
        /// <param name="_str">字符串</param>
        /// <param name="encode">编码格式</param>
        /// <returns></returns>
        private string StringToHexString(string _str)
        {
            //去掉空格
            _str = _str.Replace(" ", "");
            string[] arr = _str.Split('H');
            //定义一个string类型的变量，用于存储转换后的值。
            string result = string.Empty;
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = "0x" + arr[i].Substring(0, 2) + ",";
                if (i == arr.Length - 1)
                {
                    arr[i] = "0x" + arr[i].Substring(0, 2);
                }
                result += arr[i];
            }
            return result;
        }

        public static Byte[] string_2_BCD(string strTemp)
        {
            
            try
            {
                if (Convert.ToBoolean(strTemp.Length%1))
                {
                    strTemp = "0" + strTemp;
                }
                Byte[] arrTemp = new Byte[strTemp.Length/2];
                for (int i = 0; i < strTemp.Length / 2; i++)
                {
                    arrTemp[i] = (Byte)((strTemp[i*2]-'0')<<4| (strTemp[i * 2+1] - '0'));
                }
                return arrTemp;
            }
            catch (Exception)
            {

                return null;
            }
            
        }

        public static string DateTime2String(DateTime dateTime)
        {
            StringBuilder sb = new StringBuilder();
            //sb.Append("1C");
            sb.Append(string.Format("{0:X4}", dateTime.Year));
            sb.Append(string.Format("{0:X2}", dateTime.Month));
            sb.Append(string.Format("{0:X2}", dateTime.Day));
            sb.Append(string.Format("{0:X2}", dateTime.Hour));
            sb.Append(string.Format("{0:X2}", dateTime.Minute));
            sb.Append(string.Format("{0:X2}", dateTime.Second));
            return sb.ToString();
        }

        public static string DateTimeMill2String(DateTime dateTime)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format("{0:X4}", dateTime.Year));
            sb.Append(string.Format("{0:X2}", dateTime.Month));
            sb.Append(string.Format("{0:X2}", dateTime.Day));
            sb.Append(string.Format("{0:X2}", (int)dateTime.DayOfWeek + 1));
            sb.Append(string.Format("{0:X2}", dateTime.Hour));
            sb.Append(string.Format("{0:X2}", dateTime.Minute));
            sb.Append(string.Format("{0:X2}", dateTime.Second));
            sb.Append(string.Format("{0:X4}", dateTime.Millisecond));
            return sb.ToString();
        }
    }
}
